<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../../js/vue.js"></script>
</head>

<body>
    <div id="app">
        <child :num="num" @getparent="getparent" ref="child"></child>
        <button @click="getchild">父找子要</button>
    </div>
</body>
<template id="child">
    <div>
        <h3>来自父元素的数据 {{this.$attrs.num}}</h3>
        <h3>来自父元素的数据 {{this.$parent.num}}</h3>
        <h3><input type="text" v-model="msg"><button @click="getparent">子传父</button></h3>
    </div>
</template>
<script>
    let child = {
        template: "#child",
        data() {
            return {
                msg: "",
            }
        },
        created() {
            console.log(this);
        },
        methods: {
            getparent() {
                this.$emit("getparent",this.msg);
                console.log(this);
            }
        }
    }
    new Vue({
        el: "#app",
        data: {
            num: 10,
        },
        methods: {
            getparent(msg) {
                alert(msg);
            },
            getchild() {
                console.log(this.$refs.child.msg);
            }

        },
        components: {
            child,
        }
    })
</script>

</html>